// pages/add/sell.js

var config = require('../../config')
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array1: ['东区', '西区', ' 南区', ' 北区', ' 中北'],
    objectArray1: [
      {
        id: 0,
        name: '东区'
      },
      {
        id: 1,
        name: '西区'
      },
      {
        id: 2,
        name: '南区'
      },
      {
        id: 3,
        name: '东区'
      },
      {
        id: 4,
        name: '中北'
      }
    ],
    array2: ['生活', '学习', '娱乐'],
    objectArray2: [
      {
        id: 0,
        name: '生活'
      },
      {
        id: 1,
        name: '学习'
      },
      {
        id: 2,
        name: '娱乐'
      }
    ],
    array3: ['qq', 'wechat', 'phone'],
    objectArray3: [
      {
        id: 0,
        name: 'qq'
      },
      {
        id: 1,
        name: 'wechat'
      },
      {
        id: 2,
        name: 'phone'
      }
    ],
    index: null,
    index2: null,
    index3: null,
    schoolId: null,
    partId: null,
    contectId:null,
    jobname: null,
    jobadress:null,
    jobdetail:null,
    date: "",
    time: "",
    price: null,
    contect: null,
    userInfo: {},
    logged: false,
    openId: null
  },

  jobname:function(e){
    this.setData({
      jobname: e.detail.value
    })
  },

  jobadress:function(e){
    this.setData({
      jobadress: e.detail.value
    })
  },

  jobdetail:function(e){
    this.setData({
      jobdetail:e.detail.value
    })
  },

  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      schoolId: e.detail.value
    })
  },

  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      partId: e.detail.value
    })
  },

  bindPickerChange3: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var page = this;
    this.setData({
      index3: e.detail.value,
      contectId: page.data.objectArray3[e.detail.value].name
    })
  },

  price: function (e) {
    this.setData({
      price: e.detail.value
    })
  },

  contect: function (e) {
    this.setData({
      contect: e.detail.value
    })
  },


  commit: function (e) {
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      mask: true
    })
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/Sinput`,
      data: {
        sellername: _this.data.userInfo.nickName,
        schoolId: _this.data.schoolId,
        partId: _this.data.partId,
        jobname: _this.data.jobname,
        jobadress: _this.data.jobadress,
        jobdetail: _this.data.jobdetail,
        time: _this.data.date+" "+_this.data.time,
        price:_this.data.price,
        contect: _this.data.contect,
        contectId: _this.data.contectId,
        openId: _this.data.openId
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        if (e.data == "success") {
          wx.showToast({
            title: '提交完成',
            icon: 'success',
            mask: true
          });
          _this.setData({
            index: null,
            index2: null,
            schoolId: null,
            partId: null,
            jobname: null,
            jobadress: null,
            jobdetail: null,
            date: "",
            time: "",
            price: null,
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            mask: true
          })
          
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          mask: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("ok");
    console.log(app.globalData.openid);
    this.setData({
      openId: app.globalData.openid,
      userInfo: app.globalData.userInfo,
      logged: app.globalData.logged
    });
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/Userinfo`,
      data: {
        openId: _this.data.openId
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        console.log(e);
        var a;
        if(e.data.contectId=='qq'){
          a=0;
        }else if(e.data.contectId=='wechat'){
          a=1;
        }else{
          a=2;
        }
        _this.setData({
          contectId: e.data.contectId,
          contect: e.data.contect,
          index3:a
        })
      }
    })
    // var _this = this;
    // wx.getSetting({
    // 	success: function (res) {
    // 		if (res.authSetting['scope.userInfo']) {
    // 			// 已经授权，可以直接调用 getUserInfo 获取头像昵称
    // 			wx.getUserInfo({
    // 				success: function (res) {
    // 					console.log(res.userInfo)
    // 					_this.setData({
    // 						userInfo: res.userInfo,
    // 						logged: true
    // 					})
    // 				}
    // 			})
    // 		}
    // 	}
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  jump:function(){
    wx.navigateTo({
      url: 'add'
    })
  }
})